package P53;

/**
 * @Author DJ同学
 * @Date 2021/2/22 9:12
 * @Version 1.0
 * @Name 最大子序和
 * @Problem https://leetcode-cn.com/problems/maximum-subarray/
 * 统一处理
 */
public class Solution2 {
    public int maxSubArray(int[] nums) {
        int curSum = 0;
        int maxSum = nums[0];//处理全是负数的情况
        for(int i=0;i<nums.length;i++){
            curSum+=nums[i];
            if(curSum>maxSum){
                maxSum=curSum;
            }
            if(curSum<0){
                curSum=0;
            }
        }
        return maxSum;
    }
}
